home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / gnu / a2_0bEmacs_bin.lha / Emacs-19.25 / info / emacs-7 (.txt) < prev    next >
GNU Info File  |  1992-02-21  |  50KB  |  906 lines

  1. This is Info file ../info/emacs, produced by Makeinfo-1.43 from the
  2. input file emacs.tex.
  3.    This file documents the GNU Emacs editor.
  4.    Copyright (C) 1985, 1986, 1988 Richard M. Stallman.
  5.    Permission is granted to make and distribute verbatim copies of
  6. this manual provided the copyright notice and this permission notice
  7. are preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the sections entitled "The GNU Manifesto", "Distribution" and "GNU
  11. General Public License" are included exactly as in the original, and
  12. provided that the entire resulting derived work is distributed under
  13. the terms of a permission notice identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the sections entitled "The GNU Manifesto",
  17. "Distribution" and "GNU General Public License" may be included in a
  18. translation approved by the author instead of in the original English.
  19. File: emacs,  Node: Paragraphs,  Next: Pages,  Prev: Sentences,  Up: Text
  20. Paragraphs
  21. ==========
  22.    The Emacs commands for manipulating paragraphs are also `Meta-'
  23. keys.
  24. `M-['
  25.      Move back to previous paragraph beginning 
  26.      (`backward-paragraph').
  27. `M-]'
  28.      Move forward to next paragraph end (`forward-paragraph').
  29. `M-h'
  30.      Put point and mark around this or next paragraph
  31.      (`mark-paragraph').
  32.    `Meta-[' moves to the beginning of the current or previous
  33. paragraph, while `Meta-]' moves to the end of the current or next
  34. paragraph.  Blank lines and text formatter command lines separate
  35. paragraphs and are not part of any paragraph.  Also, an indented line
  36. starts a new paragraph.
  37.    In major modes for programs (as opposed to Text mode), paragraphs
  38. begin and end only at blank lines.  This makes the paragraph commands
  39. continue to be useful even though there are no paragraphs per se.
  40.    When there is a fill prefix, then paragraphs are delimited by all
  41. lines which don't start with the fill prefix.  *Note Filling::.
  42.    When you wish to operate on a paragraph, you can use the command
  43. `Meta-h' (`mark-paragraph') to set the region around it.  This command
  44. puts point at the beginning and mark at the end of the paragraph point
  45. was in.  If point is between paragraphs (in a run of blank lines, or
  46. at a boundary), the paragraph following point is surrounded by point
  47. and mark.  If there are blank lines preceding the first line of the
  48. paragraph, one of these blank lines is included in the region.  Thus,
  49. for example, `M-h C-w' kills the paragraph around or after point.
  50.    The precise definition of a paragraph boundary is controlled by the
  51. variables `paragraph-separate' and `paragraph-start'.  The value of
  52. `paragraph-start' is a regexp that should match any line that either
  53. starts or separates paragraphs.  The value of `paragraph-separate' is
  54. another regexp that should match only lines that separate paragraphs
  55. without being part of any paragraph.  Lines that start a new paragraph
  56. and are contained in it must match both regexps.  For example,
  57. normally `paragraph-start' is `"^[ \t\n\f]"' and `paragraph-separate'
  58. is `"^[ \t\f]*$"'.
  59.    Normally it is desirable for page boundaries to separate paragraphs. 
  60. The default values of these variables recognize the usual separator for
  61. pages.
  62. File: emacs,  Node: Pages,  Next: Filling,  Prev: Paragraphs,  Up: Text
  63. Pages
  64. =====
  65.    Files are often thought of as divided into "pages" by the
  66. "formfeed" character (ASCII Control-L, octal code 014).  For example,
  67. if a file is printed on a line printer, each page of the file, in this
  68. sense, will start on a new page of paper.  Emacs treats a
  69. page-separator character just like any other character.  It can be
  70. inserted with `C-q C-l', or deleted with DEL.  Thus, you are free to
  71. paginate your file or not.  However, since pages are often meaningful
  72. divisions of the file, commands are provided to move over them and
  73. operate on them.
  74. `C-x ['
  75.      Move point to previous page boundary (`backward-page').
  76. `C-x ]'
  77.      Move point to next page boundary (`forward-page').
  78. `C-x C-p'
  79.      Put point and mark around this page (or another page)
  80.      (`mark-page').
  81. `C-x l'
  82.      Count the lines in this page (`count-lines-page').
  83.    The `C-x [' (`backward-page') command moves point to immediately
  84. after the previous page delimiter.  If point is already right after a
  85. page delimiter, it skips that one and stops at the previous one.  A
  86. numeric argument serves as a repeat count.  The `C-x ]'
  87. (`forward-page') command moves forward past the next page delimiter.
  88.    The `C-x C-p' command (`mark-page') puts point at the beginning of
  89. the current page and the mark at the end.  The page delimiter at the
  90. end is included (the mark follows it).  The page delimiter at the
  91. front is excluded (point follows it).  This command can be followed by
  92. `C-w' to kill a page which is to be moved elsewhere.  If it is
  93. inserted after a page delimiter, at a place where `C-x ]' or `C-x ['
  94. would take you, then the page will be properly delimited before and
  95. after once again.
  96.    A numeric argument to `C-x C-p' is used to specify which page to go
  97. to, relative to the current one.  Zero means the current page.  One
  98. means the next page, and -1 means the previous one.
  99.    The `C-x l' command (`count-lines-page') is good for deciding where
  100. to break a page in two.  It prints in the echo area the total number
  101. of lines in the current page, and then divides it up into those
  102. preceding the current line and those following, as in
  103.      Page has 96 (72+25) lines
  104. Notice that the sum is off by one; this is correct if point is not at
  105. the beginning of a line.
  106.    The variable `page-delimiter' should have as its value a regexp that
  107. matches the beginning of a line that separates pages.  This is what
  108. defines where pages begin.  The normal value of this variable is
  109. `"^\f"', which matches a formfeed character at the beginning of a line.
  110. File: emacs,  Node: Filling,  Next: Case,  Prev: Pages,  Up: Text
  111. Filling Text
  112. ============
  113.    With Auto Fill mode, text can be "filled" (broken up into lines that
  114. fit in a specified width) as you insert it.  If you alter existing
  115. text it may no longer be properly filled; then explicit commands for
  116. filling can be used.
  117. * Menu:
  118. * Auto Fill::      Auto Fill mode breaks long lines automatically.
  119. * Fill Commands:: Commands to refill paragraphs and center lines.
  120. * Fill Prefix::   Filling when every line is indented or in a comment, etc.
  121. File: emacs,  Node: Auto Fill,  Next: Fill Commands,  Prev: Filling,  Up: Filling
  122. Auto Fill Mode
  123. --------------
  124.    "Auto Fill" mode is a minor mode in which lines are broken
  125. automatically when they become too wide.  Breaking happens only when
  126. you type a SPC or RET.
  127. `M-x auto-fill-mode'
  128.      Enable or disable Auto Fill mode.
  129. `SPC'
  130. `RET'
  131.      In Auto Fill mode, break lines when appropriate.
  132.    `M-x auto-fill-mode' turns Auto Fill mode on if it was off, or off
  133. if it was on.  With a positive numeric argument it always turns Auto
  134. Fill mode on, and with a negative argument always turns it off.  You
  135. can see when Auto Fill mode is in effect by the presence of the word
  136. `Fill' in the mode line, inside the parentheses.  Auto Fill mode is a
  137. minor mode, turned on or off for each buffer individually.  *Note
  138. Minor Modes::.
  139.    In Auto Fill mode, lines are broken automatically at spaces when
  140. they get longer than the desired width.  Line breaking and
  141. rearrangement takes place only when you type SPC or RET.  If you wish
  142. to insert a space or newline without permitting line-breaking, type
  143. `C-q SPC' or `C-q LFD' (recall that a newline is really a linefeed). 
  144. Also, `C-o' inserts a newline without line breaking.
  145.    Auto Fill mode works well with Lisp mode, because when it makes a
  146. new line in Lisp mode it indents that line with TAB.  If a line ending
  147. in a comment gets too long, the text of the comment is split into two
  148. comment lines.  Optionally new comment delimiters are inserted at the
  149. end of the first line and the beginning of the second so that each
  150. line is a separate comment; the variable `comment-multi-line' controls
  151. the choice (*note Comments::.).
  152.    Auto Fill mode does not refill entire paragraphs.  It can break
  153. lines but cannot merge lines.  So editing in the middle of a paragraph
  154. can result in a paragraph that is not correctly filled.  The easiest
  155. way to make the paragraph properly filled again is usually with the
  156. explicit fill commands.
  157.    Many users like Auto Fill mode and want to use it in all text files. 
  158. The section on init files says how to arrange this permanently for
  159. yourself.  *Note Init File::.
  160. File: emacs,  Node: Fill Commands,  Next: Fill Prefix,  Prev: Auto Fill,  Up: Filling
  161. Explicit Fill Commands
  162. ----------------------
  163. `M-q'
  164.      Fill current paragraph (`fill-paragraph').
  165. `M-g'
  166.      Fill each paragraph in the region (`fill-region').
  167. `C-x f'
  168.      Set the fill column (`set-fill-column').
  169. `M-x fill-region-as-paragraph.'
  170.      Fill the region, considering it as one paragraph.
  171. `M-s'
  172.      Center a line.
  173.    To refill a paragraph, use the command `Meta-q' (`fill-paragraph').
  174.  It causes the paragraph that point is inside, or the one after point
  175. if point is between paragraphs, to be refilled.  All the line-breaks
  176. are removed, and then new ones are inserted where necessary.  `M-q'
  177. can be undone with `C-_'.  *Note Undo::.
  178.    To refill many paragraphs, use `M-g' (`fill-region'), which divides
  179. the region into paragraphs and fills each of them.
  180.    `Meta-q' and `Meta-g' use the same criteria as `Meta-h' for finding
  181. paragraph boundaries (*note Paragraphs::.).  For more control, you can
  182. use `M-x fill-region-as-paragraph', which refills everything between
  183. point and mark.  This command recognizes only blank lines as paragraph
  184. separators.
  185.    A numeric argument to `M-g' or `M-q' causes it to "justify" the
  186. text as well as filling it.  This means that extra spaces are inserted
  187. to make the right margin line up exactly at the fill column.  To
  188. remove the extra spaces, use `M-q' or `M-g' with no argument.
  189.    The command `Meta-s' (`center-line') centers the current line
  190. within the current fill column.  With an argument, it centers several
  191. lines individually and moves past them.
  192.    The maximum line width for filling is in the variable `fill-column'. 
  193. Altering the value of `fill-column' makes it local to the current
  194. buffer; until that time, the default value is in effect.  The default
  195. is initially 70.  *Note Locals::.
  196.    The easiest way to set `fill-column' is to use the command `C-x f'
  197. (`set-fill-column').  With no argument, it sets `fill-column' to the
  198. current horizontal position of point.  With a numeric argument, it
  199. uses that as the new fill column.
  200. File: emacs,  Node: Fill Prefix,  Prev: Fill Commands,  Up: Filling
  201. The Fill Prefix
  202. ---------------
  203.    To fill a paragraph in which each line starts with a special marker
  204. (which might be a few spaces, giving an indented paragraph), use the
  205. "fill prefix" feature.  The fill prefix is a string which Emacs expects
  206. every line to start with, and which is not included in filling.
  207. `C-x .'
  208.      Set the fill prefix (`set-fill-prefix').
  209. `M-q'
  210.      Fill a paragraph using current fill prefix (`fill-paragraph').
  211. `M-x fill-individual-paragraphs'
  212.      Fill the region, considering each change of indentation as
  213.      starting a new paragraph.
  214.    To specify a fill prefix, move to a line that starts with the
  215. desired prefix, put point at the end of the prefix, and give the
  216. command `C-x .' (`set-fill-prefix').  That's a period after the `C-x'.
  217.  To turn off the fill prefix, specify an empty prefix: type `C-x .'
  218. with point at the beginning of a line.
  219.    When a fill prefix is in effect, the fill commands remove the fill
  220. prefix from each line before filling and insert it on each line after
  221. filling.  The fill prefix is also inserted on new lines made
  222. automatically by Auto Fill mode.  Lines that do not start with the
  223. fill prefix are considered to start paragraphs, both in `M-q' and the
  224. paragraph commands; this is just right if you are using paragraphs
  225. with hanging indentation (every line indented except the first one). 
  226. Lines which are blank or indented once the prefix is removed also
  227. separate or start paragraphs; this is what you want if you are writing
  228. multi-paragraph comments with a comment delimiter on each line.
  229.    The fill prefix is stored in the variable `fill-prefix'.  Its value
  230. is a string, or `nil' when there is no fill prefix.  This is a
  231. per-buffer variable; altering the variable affects only the current
  232. buffer, but there is a default value which you can change as well. 
  233. *Note Locals::.
  234.    Another way to use fill prefixes is through `M-x
  235. fill-individual-paragraphs'.  This function divides the region into
  236. groups of consecutive lines with the same amount and kind of
  237. indentation and fills each group as a paragraph using its indentation
  238. as a fill prefix.
  239. File: emacs,  Node: Case,  Prev: Filling,  Up: Text
  240. Case Conversion Commands
  241. ========================
  242.    Emacs has commands for converting either a single word or any
  243. arbitrary range of text to upper case or to lower case.
  244. `M-l'
  245.      Convert following word to lower case (`downcase-word').
  246. `M-u'
  247.      Convert following word to upper case (`upcase-word').
  248. `M-c'
  249.      Capitalize the following word (`capitalize-word').
  250. `C-x C-l'
  251.      Convert region to lower case (`downcase-region').
  252. `C-x C-u'
  253.      Convert region to upper case (`upcase-region').
  254.    The word conversion commands are the most useful.  `Meta-l'
  255. (`downcase-word') converts the word after point to lower case, moving
  256. past it.  Thus, repeating `Meta-l' converts successive words. 
  257. `Meta-u' (`upcase-word') converts to all capitals instead, while
  258. `Meta-c' (`capitalize-word') puts the first letter of the word into
  259. upper case and the rest into lower case.  All these commands convert
  260. several words at once if given an argument.  They are especially
  261. convenient for converting a large amount of text from all upper case
  262. to mixed case, because you can move through the text using `M-l',
  263. `M-u' or `M-c' on each word as appropriate, occasionally using `M-f'
  264. instead to skip a word.
  265.    When given a negative argument, the word case conversion commands
  266. apply to the appropriate number of words before point, but do not move
  267. point.  This is convenient when you have just typed a word in the
  268. wrong case: you can give the case conversion command and continue
  269. typing.
  270.    If a word case conversion command is given in the middle of a word,
  271. it applies only to the part of the word which follows point.  This is
  272. just like what `Meta-d' (`kill-word') does.  With a negative argument,
  273. case conversion applies only to the part of the word before point.
  274.    The other case conversion commands are `C-x C-u' (`upcase-region')
  275. and `C-x C-l' (`downcase-region'), which convert everything between
  276. point and mark to the specified case.  Point and mark do not move.
  277. File: emacs,  Node: Programs,  Next: Running,  Prev: Text,  Up: Top
  278. Editing Programs
  279. ****************
  280.    Emacs has many commands designed to understand the syntax of
  281. programming languages such as Lisp and C.  These commands can
  282.    * Move over or kill balanced expressions or "sexps" (*note
  283.      Lists::.).
  284.    * Move over or mark top-level balanced expressions ("defuns", in
  285.      Lisp; functions, in C).
  286.    * Show how parentheses balance (*note Matching::.).
  287.    * Insert, kill or align comments (*note Comments::.).
  288.    * Follow the usual indentation conventions of the language (*note
  289.      Grinding::.).
  290.    The commands for words, sentences and paragraphs are very useful in
  291. editing code even though their canonical application is for editing
  292. human language text.  Most symbols contain words (*note Words::.);
  293. sentences can be found in strings and comments (*note Sentences::.). 
  294. Paragraphs per se are not present in code, but the paragraph commands
  295. are useful anyway, because Lisp mode and C mode define paragraphs to
  296. begin and end at blank lines (*note Paragraphs::.).  Judicious use of
  297. blank lines to make the program clearer will also provide interesting
  298. chunks of text for the paragraph commands to work on.
  299.    The selective display feature is useful for looking at the overall
  300. structure of a function (*note Selective Display::.).  This feature
  301. causes only the lines that are indented less than a specified amount
  302. to appear on the screen.
  303. * Menu:
  304. * Program Modes::       Major modes for editing programs.
  305. * Lists::               Expressions with balanced parentheses.
  306.                          There are editing commands to operate on them.
  307. * Defuns::              Each program is made up of separate functions.
  308.                          There are editing commands to operate on them.
  309. * Grinding::            Adjusting indentation to show the nesting.
  310. * Matching::            Insertion of a close-delimiter flashes matching open.
  311. * Comments::            Inserting, illing and aligning comments.
  312. * Balanced Editing::    Inserting two matching parentheses at once, etc.
  313. * Lisp Completion::     Completion on symbol names in Lisp code.
  314. * Documentation::       Getting documentation of functions you plan to call.
  315. * Change Log::          Maintaining a change history for your program.
  316. * Tags::                Go direct to any function in your program in one
  317.                          command.  Tags remembers which file it is in.
  318. * Fortran::        Fortran mode and its special features.
  319. File: emacs,  Node: Program Modes,  Next: Lists,  Prev: Programs,  Up: Programs
  320. Major Modes for Programming Languages
  321. =====================================
  322.    Emacs also has major modes for the programming languages Lisp,
  323. Scheme (a variant of Lisp), C, Fortran and Muddle.  Ideally, a major
  324. mode should be implemented for each programming language that you
  325. might want to edit with Emacs; but often the mode for one language can
  326. serve for other syntactically similar languages.  The language modes
  327. that exist are those that someone decided to take the trouble to write.
  328.    There are several forms of Lisp mode, which differ in the way they
  329. interface to Lisp execution.  *Note Lisp Modes::.
  330.    Each of the programming language modes defines the TAB key to run
  331. an indentation function that knows the indentation conventions of that
  332. language and updates the current line's indentation accordingly.  For
  333. example, in C mode TAB is bound to `c-indent-line'.  LFD is normally
  334. defined to do RET followed by TAB; thus, it too indents in a
  335. mode-specific fashion.
  336.    In most programming languages, indentation is likely to vary from
  337. line to line.  So the major modes for those languages rebind DEL to
  338. treat a tab as if it were the equivalent number of spaces (using the
  339. command `backward-delete-char-untabify').  This makes it possible to
  340. rub out indentation one column at a time without worrying whether it
  341. is made up of spaces or tabs.  Use `C-b C-d' to delete a tab character
  342. before point, in these modes.
  343.    Programming language modes define paragraphs to be separated only by
  344. blank lines, so that the paragraph commands remain useful.  Auto Fill
  345. mode, if enabled in a programming language major mode, indents the new
  346. lines which it creates.
  347.    Turning on a major mode calls a user-supplied function called the
  348. "mode hook", which is the value of a Lisp variable.  For example,
  349. turning on C mode calls the value of the variable `c-mode-hook' if
  350. that value exists and is non-`nil'.  Mode hook variables for other
  351. programming language modes include `lisp-mode-hook',
  352. `emacs-lisp-mode-hook', `lisp-interaction-mode-hook',
  353. `scheme-mode-hook' and `muddle-mode-hook'.  The mode hook function
  354. receives no arguments.
  355. File: emacs,  Node: Lists,  Next: Defuns,  Prev: Program Modes,  Up: Programs
  356. Lists and Sexps
  357. ===============
  358.    By convention, Emacs keys for dealing with balanced expressions are
  359. usually `Control-Meta-' characters.  They tend to be analogous in
  360. function to their `Control-' and `Meta-' equivalents.  These commands
  361. are usually thought of as pertaining to expressions in programming
  362. languages, but can be useful with any language in which some sort of
  363. parentheses exist (including English).
  364.    These commands fall into two classes.  Some deal only with "lists"
  365. (parenthetical groupings).  They see nothing except parentheses,
  366. brackets, braces (whichever ones must balance in the language you are
  367. working with), and escape characters that might be used to quote those.
  368.    The other commands deal with expressions or "sexps".  The word
  369. `sexp' is derived from "s-expression", the ancient term for an
  370. expression in Lisp.  But in Emacs, the notion of `sexp' is not limited
  371. to Lisp.  It refers to an expression in whatever language your program
  372. is written in.  Each programming language has its own major mode,
  373. which customizes the syntax tables so that expressions in that
  374. language count as sexps.
  375.    Sexps typically include symbols, numbers, and string constants, as
  376. well as anything contained in parentheses, brackets or braces.
  377.    In languages that use prefix and infix operators, such as C, it is
  378. not possible for all expressions to be sexps.  For example, C mode
  379. does not recognize `foo + bar' as a sexp, even though it is a C
  380. expression; it recognizes `foo' as one sexp and `bar' as another, with
  381. the `+' as punctuation between them.  This is a fundamental ambiguity:
  382. both `foo + bar' and `foo' are legitimate choices for the sexp to move
  383. over if point is at the `f'.  Note that `(foo + bar)' is a sexp in C
  384. mode.
  385.    Some languages have obscure forms of syntax for expressions that
  386. nobody has bothered to make Emacs understand properly.
  387. `C-M-f'
  388.      Move forward over a sexp (`forward-sexp').
  389. `C-M-b'
  390.      Move backward over a sexp (`backward-sexp').
  391. `C-M-k'
  392.      Kill sexp forward (`kill-sexp').
  393. `C-M-u'
  394.      Move up and backward in list structure (`backward-up-list').
  395. `C-M-d'
  396.      Move down and forward in list structure (`down-list').
  397. `C-M-n'
  398.      Move forward over a list (`forward-list').
  399. `C-M-p'
  400.      Move backward over a list (`backward-list').
  401. `C-M-t'
  402.      Transpose expressions (`transpose-sexps').
  403. `C-M-@'
  404.      Put mark after following expression (`mark-sexp').
  405.    To move forward over a sexp, use `C-M-f' (`forward-sexp').  If the
  406. first significant character after point is an opening delimiter (`('
  407. in Lisp; `(', `[' or `{' in C), `C-M-f' moves past the matching
  408. closing delimiter.  If the character begins a symbol, string, or
  409. number, `C-M-f' moves over that.  If the character after point is a
  410. closing delimiter, `C-M-f' just moves past it.  (This last is not
  411. really moving across a sexp; it is an exception which is included in
  412. the definition of `C-M-f' because it is as useful a behavior as anyone
  413. can think of for that situation.)
  414.    The command `C-M-b' (`backward-sexp') moves backward over a sexp. 
  415. The detailed rules are like those above for `C-M-f', but with
  416. directions reversed.  If there are any prefix characters (singlequote,
  417. backquote and comma, in Lisp) preceding the sexp, `C-M-b' moves back
  418. over them as well.
  419.    `C-M-f' or `C-M-b' with an argument repeats that operation the
  420. specified number of times; with a negative argument, it moves in the
  421. opposite direction.
  422.    The sexp commands move across comments as if they were whitespace,
  423. in languages such as C where the comment-terminator can be recognized.
  424.  In Lisp, and other languages where comments run until the end of a
  425. line, it is very difficult to ignore comments when parsing backwards;
  426. therefore, in such languages the sexp commands treat the text of
  427. comments as if it were code.
  428.    Killing a sexp at a time can be done with `C-M-k' (`kill-sexp'). 
  429. `C-M-k' kills the characters that `C-M-f' would move over.
  430.    The "list commands" move over lists like the sexp commands but skip
  431. blithely over any number of other kinds of sexps (symbols, strings,
  432. etc).  They are `C-M-n' (`forward-list') and `C-M-p'
  433. (`backward-list').  The main reason they are useful is that they
  434. usually ignore comments (since the comments usually do not contain any
  435. lists).
  436.    `C-M-n' and `C-M-p' stay at the same level in parentheses, when
  437. that's possible.  To move up one (or N) levels, use `C-M-u'
  438. (`backward-up-list').  `C-M-u' moves backward up past one unmatched
  439. opening delimiter.  A positive argument serves as a repeat count; a
  440. negative argument reverses direction of motion and also requests
  441. repetition, so it moves forward and up one or more levels.
  442.    To move down in list structure, use `C-M-d' (`down-list').  In Lisp
  443. mode, where `(' is the only opening delimiter, this is nearly the same
  444. as searching for a `('.  An argument specifies the number of levels of
  445. parentheses to go down.
  446.    A somewhat random-sounding command which is nevertheless easy to
  447. use is `C-M-t' (`transpose-sexps'), which drags the previous sexp
  448. across the next one.  An argument serves as a repeat count, and a
  449. negative argument drags backwards (thus canceling out the effect of
  450. `C-M-t' with a positive argument).  An argument of zero, rather than
  451. doing nothing, transposes the sexps ending after point and the mark.
  452.    To make the region be the next sexp in the buffer, use `C-M-@'
  453. (`mark-sexp') which sets mark at the same place that `C-M-f' would
  454. move to.  `C-M-@' takes arguments like `C-M-f'.  In particular, a
  455. negative argument is useful for putting the mark at the beginning of
  456. the previous sexp.
  457.    The list and sexp commands' understanding of syntax is completely
  458. controlled by the syntax table.  Any character can, for example, be
  459. declared to be an opening delimiter and act like an open parenthesis. 
  460. *Note Syntax::.
  461. File: emacs,  Node: Defuns,  Next: Grinding,  Prev: Lists,  Up: Programs
  462. Defuns
  463. ======
  464.    In Emacs, a parenthetical grouping at the top level in the buffer is
  465. called a "defun".  The name derives from the fact that most top-level
  466. lists in a Lisp file are instances of the special form `defun', but
  467. any top-level parenthetical grouping counts as a defun in Emacs
  468. parlance regardless of what its contents are, and regardless of the
  469. programming language in use.  For example, in C, the body of a
  470. function definition is a defun.
  471. `C-M-a'
  472.      Move to beginning of current or preceding defun
  473.      (`beginning-of-defun').
  474. `C-M-e'
  475.      Move to end of current or following defun (`end-of-defun').
  476. `C-M-h'
  477.      Put region around whole current or following defun (`mark-defun').
  478.    The commands to move to the beginning and end of the current defun
  479. are `C-M-a' (`beginning-of-defun') and `C-M-e' (`end-of-defun').
  480.    If you wish to operate on the current defun, use `C-M-h'
  481. (`mark-defun') which puts point at the beginning and mark at the end
  482. of the current or next defun.  For example, this is the easiest way to
  483. get ready to move the defun to a different place in the text.  In C
  484. mode, `C-M-h' runs the function `mark-c-function', which is almost the
  485. same as `mark-defun'; the difference is that it backs up over the
  486. argument declarations, function name and returned data type so that the
  487. entire C function is inside the region.
  488.    Emacs assumes that any open-parenthesis found in the leftmost
  489. column is the start of a defun.  Therefore, never put an
  490. open-parenthesis at the left margin in a Lisp file unless it is the
  491. start of a top level list.  Never put an open-brace or other opening
  492. delimiter at the beginning of a line of C code unless it starts the
  493. body of a function.  The most likely problem case is when you want an
  494. opening delimiter at the start of a line inside a string.  To avoid
  495. trouble, put an escape character (`\', in C and Emacs Lisp, `/' in
  496. some other Lisp dialects) before the opening delimiter.  It will not
  497. affect the contents of the string.
  498.    In the remotest past, the original Emacs found defuns by moving
  499. upward a level of parentheses until there were no more levels to go
  500. up.  This always required scanning all the way back to the beginning
  501. of the buffer, even for a small function.  To speed up the operation,
  502. Emacs was changed to assume that any `(' (or other character assigned
  503. the syntactic class of opening-delimiter) at the left margin is the
  504. start of a defun.  This heuristic was nearly always right and avoided
  505. the costly scan; however, it mandated the convention described above.
  506. File: emacs,  Node: Grinding,  Next: Matching,  Prev: Defuns,  Up: Programs
  507. Indentation for Programs
  508. ========================
  509.    The best way to keep a program properly indented ("ground") is to
  510. use Emacs to re-indent it as you change it.  Emacs has commands to
  511. indent properly either a single line, a specified number of lines, or
  512. all of the lines inside a single parenthetical grouping.
  513. * Menu:
  514. * Basic Indent::
  515. * Multi-line Indent::   Commands to reindent many lines at once.
  516. * Lisp Indent::        Specifying how each Lisp function should be indented.
  517. * C Indent::        Choosing an indentation style for C code.
  518. File: emacs,  Node: Basic Indent,  Next: Multi-line Indent,  Prev: Grinding,  Up: Grinding
  519. Basic Program Indentation Commands
  520. ----------------------------------
  521. `TAB'
  522.      Adjust indentation of current line.
  523. `LFD'
  524.      Equivalent to RET followed by TAB (`newline-and-indent').
  525.    The basic indentation command is TAB, which gives the current line
  526. the correct indentation as determined from the previous lines.  The
  527. function that TAB runs depends on the major mode; it is
  528. `lisp-indent-line' in Lisp mode, `c-indent-line' in C mode, etc. 
  529. These functions understand different syntaxes for different languages,
  530. but they all do about the same thing.  TAB in any programming language
  531. major mode inserts or deletes whitespace at the beginning of the
  532. current line, independent of where point is in the line.  If point is
  533. inside the whitespace at the beginning of the line, TAB leaves it at
  534. the end of that whitespace; otherwise, TAB leaves point fixed with
  535. respect to the characters around it.
  536.    Use `C-q TAB' to insert a tab at point.
  537.    When entering a large amount of new code, use LFD
  538. (`newline-and-indent'), which is equivalent to a RET followed by a
  539. TAB.  LFD creates a blank line, and then gives it the appropriate
  540. indentation.
  541.    TAB indents the second and following lines of the body of an
  542. parenthetical grouping each under the preceding one; therefore, if you
  543. alter one line's indentation to be nonstandard, the lines below will
  544. tend to follow it.  This is the right behavior in cases where the
  545. standard result of TAB is unaesthetic.
  546.    Remember that an open-parenthesis, open-brace or other opening
  547. delimiter at the left margin is assumed by Emacs (including the
  548. indentation routines) to be the start of a function.  Therefore, you
  549. must never have an opening delimiter in column zero that is not the
  550. beginning of a function, not even inside a string.  This restriction
  551. is vital for making the indentation commands fast; you must simply
  552. accept it.  *Note Defuns::, for more information on this.
  553. File: emacs,  Node: Multi-line Indent,  Next: Lisp Indent,  Prev: Basic Indent,  Up: Grinding
  554. Indenting Several Lines
  555. -----------------------
  556.    When you wish to re-indent several lines of code which have been
  557. altered or moved to a different level in the list structure, you have
  558. several commands available.
  559. `C-M-q'
  560.      Re-indent all the lines within one list (`indent-sexp').
  561. `C-u TAB'
  562.      Shift an entire list rigidly sideways so that its first line is
  563.      properly indented.
  564. `C-M-\'
  565.      Re-indent all lines in the region (`indent-region').
  566.    You can re-indent the contents of a single list by positioning point
  567. before the beginning of it and typing `C-M-q' (`indent-sexp' in Lisp
  568. mode, `indent-c-exp' in C mode; also bound to other suitable functions
  569. in other modes).  The indentation of the line the sexp starts on is
  570. not changed; therefore, only the relative indentation within the list,
  571. and not its position, is changed.  To correct the position as well,
  572. type a TAB before the `C-M-q'.
  573.    If the relative indentation within a list is correct but the
  574. indentation of its beginning is not, go to the line the list begins on
  575. and type `C-u TAB'.  When TAB is given a numeric argument, it moves
  576. all the lines in the grouping starting on the current line sideways
  577. the same amount that the current line moves.  It is clever, though,
  578. and does not move lines that start inside strings, or C preprocessor
  579. lines when in C mode.
  580.    Another way to specify the range to be re-indented is with point and
  581. mark.  The command `C-M-\' (`indent-region') applies TAB to every line
  582. whose first character is between point and mark.
  583. File: emacs,  Node: Lisp Indent,  Next: C Indent,  Prev: Multi-line Indent,  Up: Grinding
  584. Customizing Lisp Indentation
  585. ----------------------------
  586.    The indentation pattern for a Lisp expression can depend on the
  587. function called by the expression.  For each Lisp function, you can
  588. choose among several predefined patterns of indentation, or define an
  589. arbitrary one with a Lisp program.
  590.    The standard pattern of indentation is as follows: the second line
  591. of the expression is indented under the first argument, if that is on
  592. the same line as the beginning of the expression; otherwise, the
  593. second line is indented underneath the function name.  Each following
  594. line is indented under the previous line whose nesting depth is the
  595. same.
  596.    If the variable `lisp-indent-offset' is non-`nil', it overrides the
  597. usual indentation pattern for the second line of an expression, so that
  598. such lines are always indented `lisp-indent-offset' more columns than
  599. the containing list.
  600.    The standard pattern is overridded for certain functions.  Functions
  601. whose names start with `def' always indent the second line by
  602. `lisp-body-indention' extra columns beyond the open-parenthesis
  603. starting the expression.
  604.    The standard pattern can be overridden in various ways for
  605. individual functions, according to the `lisp-indent-hook' property of
  606. the function name.  There are four possibilities for this property:
  607. `nil'
  608.      This is the same as no property; the standard indentation pattern
  609.      is used.
  610. `defun'
  611.      The pattern used for function names that start with `def' is used
  612.      for this function also.
  613. a number, NUMBER
  614.      The first NUMBER arguments of the function are "distinguished"
  615.      arguments; the rest are considered the "body" of the expression. 
  616.      A line in the expression is indented according to whether the
  617.      first argument on it is distinguished or not.  If the argument is
  618.      part of the body, the line is indented `lisp-body-indent' more
  619.      columns than the open-parenthesis starting the containing
  620.      expression.  If the argument is distinguished and is either the
  621.      first or second argument, it is indented twice that many extra
  622.      columns.  If the argument is distinguished and not the first or
  623.      second argument, the standard pattern is followed for that line.
  624. a symbol, SYMBOL
  625.      SYMBOL should be a function name; that function is called to
  626.      calculate the indentation of a line within this expression.  The
  627.      function receives two arguments:
  628.     STATE
  629.           The value returned by `parse-partial-sexp' (a Lisp primitive
  630.           for indentation and nesting computation) when it parses up
  631.           to the beginning of this line.
  632.     POS
  633.           The position at which the line being indented begins.
  634.      It should return either a number, which is the number of columns
  635.      of indentation for that line, or a list whose car is such a
  636.      number.  The difference between returning a number and returning
  637.      a list is that a number says that all following lines at the same
  638.      nesting level should be indented just like this one; a list says
  639.      that following lines might call for different indentations.  This
  640.      makes a difference when the indentation is being computed by
  641.      `C-M-q'; if the value is a number, `C-M-q' need not recalculate
  642.      indentation for the following lines until the end of the list.
  643. File: emacs,  Node: C Indent,  Prev: Lisp Indent,  Up: Grinding
  644. Customizing C Indentation
  645. -------------------------
  646.    Two variables control which commands perform C indentation and when.
  647.    If `c-auto-newline' is non-`nil', newlines are inserted both before
  648. and after braces that you insert, and after colons and semicolons. 
  649. Correct C indentation is done on all the lines that are made this way.
  650.    If `c-tab-always-indent' is non-`nil', the TAB command in C mode
  651. does indentation only if point is at the left margin or within the
  652. line's indentation.  If there is non-whitespace to the left of point,
  653. then TAB just inserts a tab character in the buffer.  Normally, this
  654. variable is `nil', and TAB always reindents the current line.
  655.    C does not have anything analogous to particular function names for
  656. which special forms of indentation are desirable.  However, it has a
  657. different need for customization facilities: many different styles of
  658. C indentation are in common use.
  659.    There are six variables you can set to control the style that Emacs
  660. C mode will use.
  661. `c-indent-level'
  662.      Indentation of C statements within surrounding block.  The
  663.      surrounding block's indentation is the indentation of the line on
  664.      which the open-brace appears.
  665. `c-continued-statement-offset'
  666.      Extra indentation given to a substatement, such as the
  667.      then-clause of an if or body of a while.
  668. `c-brace-offset'
  669.      Extra indentation for line if it starts with an open brace.
  670. `c-brace-imaginary-offset'
  671.      An open brace following other text is treated as if it were this
  672.      far to the right of the start of its line.
  673. `c-argdecl-indent'
  674.      Indentation level of declarations of C function arguments.
  675. `c-label-offset'
  676.      Extra indentation for line that is a label, or case or default.
  677.    The variable `c-indent-level' controls the indentation for C
  678. statements with respect to the surrounding block.  In the example
  679.          {
  680.            foo ();
  681. the difference in indentation between the lines is `c-indent-level'. 
  682. Its standard value is 2.
  683.    If the open-brace beginning the compound statement is not at the
  684. beginning of its line, the `c-indent-level' is added to the
  685. indentation of the line, not the column of the open-brace.  For
  686. example,
  687.      if (losing) {
  688.        do_this ();
  689. One popular indentation style is that which results from setting
  690. `c-indent-level' to 8 and putting open-braces at the end of a line in
  691. this way.  I prefer to put the open-brace on a separate line.
  692.    In fact, the value of the variable `c-brace-imaginary-offset' is
  693. also added to the indentation of such a statement.  Normally this
  694. variable is zero.  Think of this variable as the imaginary position of
  695. the open brace, relative to the first nonblank character on the line. 
  696. By setting this variable to 4 and `c-indent-level' to 0, you can get
  697. this style:
  698.      if (x == y) {
  699.          do_it ();
  700.          }
  701.    When `c-indent-level' is zero, the statements inside most braces
  702. will line up right under the open brace.  But there is an exception
  703. made for braces in column zero, such as surrounding a function's body.
  704.  The statements just inside it do not go at column zero.  Instead,
  705. `c-brace-offset' and `c-continued-statement-offset' (see below) are
  706. added to produce a typical offset between brace levels, and the
  707. statements are indented that far.
  708.    `c-continued-statement-offset' controls the extra indentation for a
  709. line that starts within a statement (but not within parentheses or
  710. brackets).  These lines are usually statements that are within other
  711. statements, such as the then-clauses of `if' statements and the bodies
  712. of `while' statements.  This parameter is the difference in
  713. indentation between the two lines in
  714.      if (x == y)
  715.        do_it ();
  716. Its standard value is 2.  Some popular indentation styles correspond
  717. to a value of zero for `c-continued-statement-offset'.
  718.    `c-brace-offset' is the extra indentation given to a line that
  719. starts with an open-brace.  Its standard value is zero; compare
  720.      if (x == y)
  721.        {
  722.      if (x == y)
  723.        do_it ();
  724. if `c-brace-offset' were set to 4, the first example would become
  725.      if (x == y)
  726.            {
  727.    `c-argdecl-indent' controls the indentation of declarations of the
  728. arguments of a C function.  It is absolute: argument declarations
  729. receive exactly `c-argdecl-indent' spaces.  The standard value is 5,
  730. resulting in code like this:
  731.      char *
  732.      index (string, char)
  733.           char *string;
  734.           int char;
  735.    `c-label-offset' is the extra indentation given to a line that
  736. contains a label, a case statement, or a `default:' statement.  Its
  737. standard value is -2, resulting in code like this
  738.      switch (c)
  739.        {
  740.        case 'x':
  741. If `c-label-offset' were zero, the same code would be indented as
  742.      switch (c)
  743.        {
  744.          case 'x':
  745. This example assumes that the other variables above also have their
  746. standard values.
  747.    I strongly recommend that you try out the indentation style
  748. produced by the standard settings of these variables, together with
  749. putting open braces on separate lines.  You can see how it looks in
  750. all the C source files of GNU Emacs.
  751. File: emacs,  Node: Matching,  Next: Comments,  Prev: Grinding,  Up: Programs
  752. Automatic Display Of Matching Parentheses
  753. =========================================
  754.    The Emacs parenthesis-matching feature is designed to show
  755. automatically how parentheses match in the text.  Whenever a
  756. self-inserting character that is a closing delimiter is typed, the
  757. cursor moves momentarily to the location of the matching opening
  758. delimiter, provided that is on the screen.  If it is not on the
  759. screen, some text starting with that opening delimiter is displayed in
  760. the echo area.  Either way, you can tell what grouping is being closed
  761.    In Lisp, automatic matching applies only to parentheses.  In C, it
  762. applies to braces and brackets too.  Emacs knows which characters to
  763. regard as matching delimiters based on the syntax table, which is set
  764. by the major mode.  *Note Syntax::.
  765.    If the opening delimiter and closing delimiter are mismatched--such
  766. as in `[x)'--a warning message is displayed in the echo area.  The
  767. correct matches are specified in the syntax table.
  768.    Two variables control parenthesis match display. 
  769. `blink-matching-paren' turns the feature on or off; `nil' turns it
  770. off, but the default is `t' to turn match display on. 
  771. `blink-matching-paren-distance' specifies how many characters back to
  772. search to find the matching opening delimiter.  If the match is not
  773. found in that far, scanning stops, and nothing is displayed.  This is
  774. to prevent scanning for the matching delimiter from wasting lots of
  775. time when there is no match.  The default is 4000.
  776. File: emacs,  Node: Comments,  Next: Balanced Editing,  Prev: Matching,  Up: Programs
  777. Manipulating Comments
  778. =====================
  779.    The comment commands insert, kill and align comments.
  780. `M-;'
  781.      Insert or align comment (`indent-for-comment').
  782. `C-x ;'
  783.      Set comment column (`set-comment-column').
  784. `C-u - C-x ;'
  785.      Kill comment on current line (`kill-comment').
  786. `M-LFD'
  787.      Like RET followed by inserting and aligning a comment
  788.      (`indent-new-comment-line').
  789.    The command that creates a comment is `Meta-;'
  790. (`indent-for-comment').  If there is no comment already on the line, a
  791. new comment is created, aligned at a specific column called the
  792. "comment column".  The comment is created by inserting the string
  793. Emacs thinks comments should start with (the value of `comment-start';
  794. see below).  Point is left after that string.  If the text of the line
  795. extends past the comment column, then the indentation is done to a
  796. suitable boundary (usually, at least one space is inserted).  If the
  797. major mode has specified a string to terminate comments, that is
  798. inserted after point, to keep the syntax valid.
  799.    `Meta-;' can also be used to align an existing comment.  If a line
  800. already contains the string that starts comments, then `M-;' just moves
  801. point after it and re-indents it to the conventional place.  Exception:
  802. comments starting in column 0 are not moved.
  803.    Some major modes have special rules for indenting certain kinds of
  804. comments in certain contexts.  For example, in Lisp code, comments
  805. which start with two semicolons are indented as if they were lines of
  806. code, instead of at the comment column.  Comments which start with
  807. three semicolons are supposed to start at the left margin.  Emacs
  808. understands these conventions by indenting a double-semicolon comment
  809. using TAB, and by not changing the indentation of a triple-semicolon
  810. comment at all.
  811.      ;; This function is just an example
  812.      ;;; Here either two or three semicolons are appropriate.
  813.      (defun foo (x)
  814.      ;;; And now, the first part of the function:
  815.        ;; The following line adds one.
  816.        (1+ x))           ; This line adds one.
  817.    In C code, a comment preceded on its line by nothing but whitespace
  818. is indented like a line of code.
  819.    Even when an existing comment is properly aligned, `M-;' is still
  820. useful for moving directly to the start of the comment.
  821.    `C-u - C-x ;' (`kill-comment') kills the comment on the current
  822. line, if there is one.  The indentation before the start of the
  823. comment is killed as well.  If there does not appear to be a comment
  824. in the line, nothing is done.  To reinsert the comment on another
  825. line, move to the end of that line, do `C-y', and then do `M-;' to
  826. realign it.  Note that `C-u - C-x ;' is not a distinct key; it is `C-x
  827. ;' (`set-comment-column') with a negative argument.  That command is
  828. programmed so that when it receives a negative argument it calls
  829. `kill-comment'.  However, `kill-comment' is a valid command which you
  830. could bind directly to a key if you wanted to.
  831. Multiple Lines of Comments
  832. --------------------------
  833.    If you are typing a comment and find that you wish to continue it on
  834. another line, you can use the command `Meta-LFD'
  835. (`indent-new-comment-line'), which terminates the comment you are
  836. typing, creates a new blank line afterward, and begins a new comment
  837. indented under the old one.  When Auto Fill mode is on, going past the
  838. fill column while typing a comment causes the comment to be continued
  839. in just this fashion.  If point is not at the end of the line when
  840. `M-LFD' is typed, the text on the rest of the line becomes part of the
  841. new comment line.
  842. Options Controlling Comments
  843. ----------------------------
  844.    The comment column is stored in the variable `comment-column'.  You
  845. can set it to a number explicitly.  Alternatively, the command `C-x ;'
  846. (`set-comment-column') sets the comment column to the column point is
  847. at.  `C-u C-x ;' sets the comment column to match the last comment
  848. before point in the buffer, and then does a `Meta-;' to align the
  849. current line's comment under the previous one.  Note that `C-u - C-x ;'
  850. runs the function `kill-comment' as described above.
  851.    `comment-column' is a per-buffer variable; altering the variable
  852. affects only the current buffer, but there is a default value which
  853. you can change as well.  *Note Locals::.  Many major modes initialize
  854. this variable for the current buffer.
  855.    The comment commands recognize comments based on the regular
  856. expression that is the value of the variable `comment-start-skip'. 
  857. This regexp should not match the null string.  It may match more than
  858. the comment starting delimiter in the strictest sense of the word; for
  859. example, in C mode the value of the variable is `"/\\*+ *"', which
  860. matches extra stars and spaces after the `/*' itself.  (Note that `\\'
  861. is needed in Lisp syntax to include a `\' in the string, which is
  862. needed to deny the first star its special meaning in regexp syntax. 
  863. *Note Regexps::.)
  864.    When a comment command makes a new comment, it inserts the value of
  865. `comment-start' to begin it.  The value of `comment-end' is inserted
  866. after point, so that it will follow the text that you will insert into
  867. the comment.  In C mode, `comment-start' has the value `"/* "' and
  868. `comment-end' has the value `" */"'.
  869.    `comment-multi-line' controls how `M-LFD'
  870. (`indent-new-comment-line') behaves when used inside a comment.  If
  871. `comment-multi-line' is `nil', as it normally is, then the comment on
  872. the starting line is terminated and a new comment is started on the
  873. new following line.  If `comment-multi-line' is not `nil', then the
  874. new following line is set up as part of the same comment that was
  875. found on the starting line.  This is done by not inserting a
  876. terminator on the old line, and not inserting a starter on the new
  877. line.  In languages where multi-line comments work, the choice of
  878. value for this variable is a matter of taste.
  879.    The variable `comment-indent-hook' should contain a function that
  880. will be called to compute the indentation for a newly inserted comment
  881. or for aligning an existing comment.  It is set differently by various
  882. major modes.  The function is called with no arguments, but with point
  883. at the beginning of the comment, or at the end of a line if a new
  884. comment is to be inserted.  It should return the column in which the
  885. comment ought to start.  For example, in Lisp mode, the indent hook
  886. function bases its decision on how many semicolons begin an existing
  887. comment, and on the code in the preceding lines.
  888. File: emacs,  Node: Balanced Editing,  Next: Lisp Completion,  Prev: Comments,  Up: Programs
  889. Editing Without Unbalanced Parentheses
  890. ======================================
  891. `M-('
  892.      Put parentheses around next sexp(s) (`insert-parentheses').
  893. `M-)'
  894.      Move past next close parenthesis and re-indent
  895.      (`move-over-close-and-reindent').
  896.    The commands `M-(' (`insert-parentheses') and `M-)'
  897. (`move-over-close-and-reindent') are designed to facilitate a style of
  898. editing which keeps parentheses balanced at all times.  `M-(' inserts a
  899. pair of parentheses, either together as in `()', or, if given an
  900. argument, around the next several sexps, and leaves point after the
  901. open parenthesis.  Instead of typing `( F O O )', you can type `M-( F O
  902. O', which has the same effect except for leaving the cursor before the
  903. close parenthesis.  Then you would type `M-)', which moves past the
  904. close parenthesis, deleting any indentation preceding it (in this
  905. example there is none), and indenting with LFD after it.
  906.